//
//  ViewController.swift
//  Example
//
//  Created by Theo Chen on 2020/11/9.
//

import UIKit
import MirrorGoogleUtilities

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

/// GULAppDelegateInterceptorID: Type alias of String
public typealias GULAppDelegateInterceptorID = String

@objcMembers
open class COBAppDelegateSwizzler: GULAppDelegateSwizzler {
    
    @discardableResult
    override open class func registerAppDelegateInterceptor(_ interceptor:UIApplicationDelegate) -> GULAppDelegateInterceptorID? {
        let interceptorID = super.registerAppDelegateInterceptor(interceptor)
        
        if interceptor.responds(to: #selector(interceptor.applicationWillTerminate(_:))) {
            NotificationCenter.default.addObserver(interceptor,
                                                   selector: #selector(interceptor.applicationWillTerminate(_:)),
                                                   name: UIApplication.willTerminateNotification,
                                                   object: nil)
        }
        
        if interceptor.responds(to: #selector(interceptor.applicationDidBecomeActive(_:))) {
            NotificationCenter.default.addObserver(interceptor,
                                                   selector: #selector(interceptor.applicationDidBecomeActive(_:)),
                                                   name: UIApplication.didBecomeActiveNotification,
                                                   object: nil)
        }
        
        if interceptor.responds(to: #selector(interceptor.applicationDidEnterBackground(_:))) {
            NotificationCenter.default.addObserver(interceptor,
                                                   selector: #selector(interceptor.applicationDidEnterBackground(_:)),
                                                   name: UIApplication.didEnterBackgroundNotification,
                                                   object: nil)
        }
        
        if interceptor.responds(to: #selector(interceptor.applicationWillResignActive(_:))) {
            NotificationCenter.default.addObserver(interceptor,
                                                   selector: #selector(interceptor.applicationWillResignActive(_:)),
                                                   name: UIApplication.willResignActiveNotification,
                                                   object: nil)
        }
        
        if interceptor.responds(to: #selector(interceptor.applicationWillEnterForeground(_:))) {
            NotificationCenter.default.addObserver(interceptor,
                                                   selector: #selector(interceptor.applicationWillEnterForeground(_:)),
                                                   name: UIApplication.willEnterForegroundNotification,
                                                   object: nil)
        }
        
        return interceptorID
    }
    
    override open class func unregisterAppDelegateInterceptor(withID interceptorID:GULAppDelegateInterceptorID) {
        
        if let interceptor = GULAppDelegateSwizzler.interceptor(withID: interceptorID) {
            NotificationCenter.default.removeObserver(interceptor)
        }
        
        super.unregisterAppDelegateInterceptor(withID: interceptorID)
        
    }
}

